package com.neishenmo.sochatapp.http;


import com.neishenmo.sochatapp.util.StringConverterFactory;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;

import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

/**
 * Created by Administrator on 2016/11/22.
 */

public class RetrofitFactory
{

    private static RetrofitFactory instances;
    /**
     * 单例模式
     * @return
     */
    public static synchronized RetrofitFactory getInstances()
    {
        if (instances == null)
        {
            synchronized (RetrofitFactory.class)
            {
                if (instances == null)
                {
                    instances = new RetrofitFactory();
                }
            }
        }
        return instances;
    }



    public  Retrofit getRetrofit()
    {
        Retrofit retrofit = new Retrofit.Builder()
                .client(getNericClient())
                .addConverterFactory(StringConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                //.addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Constants.BASE_URL)
                .build();
        return retrofit;
    }

    public static OkHttpClient getNericClient() {
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request()
                                .newBuilder()
                                .addHeader("Content-Type", "application/json; charset=UTF-8")
                                .addHeader("Accept-Encoding", "gzip, deflate")
                                .addHeader("Connection", "keep-alive")
                                .addHeader("Accept", "*/*")
                                .addHeader("Cookie", "add cookies here")
                                .build();
                        return chain.proceed(request);
                    }

                })
                .build();

        return httpClient;
    }






}
